//
//  GDTrainView.m
//  GraduationDesign
//
//  Created by JiaKang.Zhong on 16/1/7.
//  Copyright © 2016年 JiaKang.Zhong. All rights reserved.
//

#import "GDTrainView.h"


@interface GDTrainView ()
@property (nonatomic,weak) UILabel * startStation;
@property (nonatomic,weak) UILabel * trainNo;
@property (nonatomic,weak) UILabel * endStation;
@property (nonatomic,weak) UILabel * trainType;
@property (nonatomic,weak) UILabel * trainStartTime;
@property (nonatomic,weak) UILabel * trainEndTime;
@property (nonatomic,weak) UILabel * ticketLabel;
@property (nonatomic,weak) UIImageView * image_view;

@end
@implementation GDTrainView

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super  initWithFrame:frame]) {
   
        [self setUpAllSubviews];
    }
    return self;
}



#pragma mark 创建页面的子空间
- (void)setUpAllSubviews{
   
    /**
     *  开始站
     */
    UILabel * startStation = [[UILabel alloc] init];
    startStation.font = [UIFont boldSystemFontOfSize:GDLabelBigFont];
    [self addSubview:startStation];
    _startStation = startStation;
    
    
    /**
     *  终点站
     */
    UILabel * endStation = [[UILabel alloc] init];
    endStation.font = [UIFont boldSystemFontOfSize:GDLabelBigFont];
    [self addSubview:endStation];
    _endStation = endStation;
    
    
    /**
     *  车次
     */
    UILabel * trainNo = [[UILabel alloc] init];
    trainNo.font = [UIFont systemFontOfSize:GDLabelSmalFont];
    [self addSubview:trainNo];
    _trainNo = trainNo;
    
    
    /**
     *  类型
     */
    UILabel * trainType = [[UILabel alloc] init];
    trainType.font = [UIFont systemFontOfSize:GDLabelSmalFont];
    [self addSubview:trainType];
    _trainType = trainType;
    
    /**
     *  发车时间
     */
    UILabel * trainStartTime = [[UILabel alloc] init];
    trainStartTime.font = [UIFont boldSystemFontOfSize:GDLabelSmalFont];
    [self addSubview:trainStartTime];
    _trainStartTime = trainStartTime;
    
    
    /**
     *  到站时间
     */
    UILabel * trainEndTime = [[UILabel alloc] init];
    trainEndTime.font = [UIFont boldSystemFontOfSize:GDLabelMidFont];
    [self addSubview:trainEndTime];
    _trainEndTime = trainEndTime;
    
    /**
     *  没有票的label
     *
     */
    UILabel * ticketLabel = [[UILabel alloc] init];
    ticketLabel.textColor = [UIColor redColor];
    ticketLabel.text = @"没有票咯,下次请早";
    ticketLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:ticketLabel];
    _ticketLabel = ticketLabel;
    
    
    /**
     *
     * 没有票的imgaeView
     */
//    UIImageView * image_view = [[UIImageView alloc] init];
//    image_view.image = [UIImage imageNamed:@"city_search_notfound_pic"];
//    [self addSubview:image_view];
//    image_view.alpha = 0.6;
//    [self sendSubviewToBack:image_view];
//    _image_view = image_view;
    
    
    /**
     *  有票的View
     *
     */
    UIView * ticketView = [[UIView alloc] init];
    [self addSubview:ticketView];
    _ticketView = ticketView;
  
}


-(void)setViewModel:(GDTrainViewModel *)viewModel{
    
    GDTrainInformationModel * informationModel = viewModel.model;
    [self setUpInformationModel:informationModel];
    
    /*     设置frame值      */
    _startStation.frame = viewModel.startStationViewFrame;
    _endStation.frame = viewModel.endStationViewFrame;
    _trainNo.frame = viewModel.trainNoViewFrame;
    _trainType.frame = viewModel.trainTypeViewFrame;
    _trainStartTime.frame = viewModel.trainStartTimeViewFrame;
    _trainEndTime.frame = viewModel.trainEndTimeViewFrame;
    if (informationModel.existTicketArray.count == 0) {
          // 没有票
        _ticketLabel.frame = viewModel.noTicketLabelViewFrame;
//        _image_view.frame = viewModel.noTicketUIImageViewFrame;
        _image_view.hidden = NO;
        _ticketLabel.hidden = NO;
        _ticketView.hidden = YES;
        self.backgroundColor = [UIColor grayColor];
        self.alpha = 0.6;
    }else{
        //  有票
        self.backgroundColor = [UIColor clearColor];
        self.alpha = 1;
        _image_view.hidden = YES;
        _ticketLabel.hidden = YES;
        _ticketView.hidden = NO;
        _ticketView.frame = viewModel.ticketViewFrame;
        [self setUpTicketModel:informationModel.existTicketArray];
    }


}



#pragma mark 设置内容
-(void)setUpInformationModel:(GDTrainInformationModel *)informationModel{

    _startStation.text = informationModel.start_station_name;
    _endStation.text = informationModel.end_station_name;
    _trainNo.text = [NSString stringWithFormat:@"车号: %@",informationModel.train_no];
    _trainType.text = [NSString stringWithFormat:@"车型: %@",informationModel.train_class_name];
    _trainStartTime.text = [NSString stringWithFormat:@"发车时间: %@",informationModel.start_time];
    _trainEndTime.text = [NSString stringWithFormat:@"到站时间: %@",informationModel.arrive_time];
    
}

#pragma mark 设置票价text
-(void) setUpTicketModel:(NSArray *) array{

    NSMutableArray * arrayM = [NSMutableArray array];
     NSMutableArray * valueArray = [NSMutableArray array];
      for (NSDictionary * dic in array) {
          for (id key in dic) {
              [arrayM addObject:key];
              [valueArray addObject:[dic objectForKey:key]];
          }
     }
    NSArray * keyArray =  [self analythArrayText:[arrayM copy]];
    
    /**
     *  重用cell时，把UIView上的所有UILable都移除掉，每一次都进行重现加载
     *  采用九宫格的形式把数据都加上去
     */
    CGFloat lableX = GDMargin;
    CGFloat lableY = GDMargin;
    CGFloat lableW = 100;
    CGFloat lableH = 30;
    for (int index = 0; index < keyArray.count; index++) {
        int row = index / 3; // 行，取决于Y值
        int col = index % 3; // 列 取决于X值
        UILabel * ticketAndtickType = [[UILabel alloc] initWithFrame:CGRectMake(lableX* col + col * lableW, lableY + lableH * row, lableW, lableH)];
        ticketAndtickType.highlighted = YES;
        ticketAndtickType.highlightedTextColor = [UIColor blueColor];
        ticketAndtickType.textColor = [UIColor blueColor];
        ticketAndtickType.textAlignment = NSTextAlignmentCenter;
        ticketAndtickType.text = [NSString stringWithFormat:@"%@:%@",keyArray[index], valueArray[index]];
        [_ticketView addSubview:ticketAndtickType];
        
    }
}

#pragma mark 将座位信息汉语化
-(NSArray *) analythArrayText:(NSArray *) array{
    NSMutableArray * arrayM = [NSMutableArray array];
    for (int i = 0 ; i < array.count; i++) {
        NSString * key = array[i];
        if([key isEqualToString:@"swz"]){
            [arrayM addObject:@"商务座"];
        }else  if([key isEqualToString:@"zy"]){
             [arrayM addObject:@"一等座"];
        }else  if([key isEqualToString:@"ze"]){
           [arrayM addObject:@"二等座"];
        }else  if([key isEqualToString:@"wz"]){
            [arrayM addObject:@"无座"];
        }else  if([key isEqualToString:@"rw"]){
            [arrayM addObject:@"软卧"];
        }
        else  if([key isEqualToString:@"tz"]){
            [arrayM addObject:@"特等座"];
        }
        else  if([key isEqualToString:@"rz"]){
            [arrayM addObject:@"软座"];
        }
        else  if([key isEqualToString:@"gr"]){
            [arrayM addObject:@"高级软卧"];
        }
        
    }
    return [arrayM copy];
}

@end
